var app = getApp();
var iFDU = require('../../utils/iFDU.js');
Page({
  data: {
    config: app.config,
    globalData: app.globalData,
    
    preferentialProductInfo: 0,
    products: []
  },

  onLoad: function (options) {
  },

  onShow: function () {
    var that = this;

    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      iFDU.api.doGet(
        'product/getProductInfo',
        {
          userId: globalData.userId,
          mainProductId: 278,
        },
        function (data) {
          that.setData({
            products: data.productView.subProductViews
          });
        }
      );

      iFDU.api.doGet(
        'product/getPreferentialProductInfoOf20',
        {
          userId: globalData.userId,
        },
        function (data) {
          that.setData({
            preferentialProductInfo: data
          });
        }
      );
    });
  },

  goBuy: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    if (index == 0) {
      if (that.data.preferentialProductInfo.isOrdered == 1) {
        if (that.data.preferentialProductInfo.orderStatus == 1 || that.data.preferentialProductInfo.orderStatus == 5) {
          wx.showModal({
            content: '已参与充1分得20元余额活动，每位用户限购一单哦！',
            confirmText: "知道了",
            cancelText: "查看订单",
            success: function (res) {
              console.log(res);
              if (res.confirm) {
              } else {
                wx.navigateTo({
                  url: '../order-detail/order-detail?orderId=' + that.data.preferentialProductInfo.orderId,
                })
              }
            }
          });
          return;
        }
      } 
    }

    var selectedProduct = that.data.products[index];
    var goods = {
      goods_id: selectedProduct.id,
      goods_count: 1,
      goods_logo: selectedProduct.logo,
      goods_title: selectedProduct.share_title,
      is_need_figure: 1,
      is_broider: 0,
      is_virtual: 1,
      goods_price: selectedProduct.channelPrice,
      sleeve_word: '',
      sleeve_word_type: 1
    };

    

    var goodsList = [goods];
    wx.setStorageSync('goodsList', JSON.stringify(goodsList));
    wx.navigateTo({
      url: '../order-confirm/order-confirm?fromCart=false&virtual=1',
    })
  },

  goRecharge: function () {
    wx.navigateTo({
      url: '../recharge-code/recharge-code',
    })
  },

  goVirGoodsList: function () {
    wx.navigateTo({
      url: '../goods-item/goods-item?goodsId=279',
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '不以优惠为目的的充值，都是耍流氓...',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/recharge/recharge',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})